import React, { Component } from 'react'
import Header from '../../components/Header/Header'
import { Link } from "react-router-dom"
import { Button,Input, List} from 'antd-mobile'
import { reqlogin } from '../../http/api'
import {
    Toast
} from "antd-mobile"
export default class login extends Component {
    constructor() {
        super()
        this.state = {
            user:{
                phone:"",
                password:""
            }
        }
    }
    login() {
        // 存一个标记

        reqlogin(this.state.user).then(res=>{
            if(res.data.code ===200){
                console.log(res.data.list);
                 Toast.show({
                    content: res.data.msg,
                })
                sessionStorage.setItem("isLogin", 1)
              
                //获取后端请求来的信息有token等 存储用户信息 //从后端请求来的token等信息转成字符串
                sessionStorage.setItem("userinfo",JSON.stringify(res.data.list))
                  // 跳转页面
                 
                this.props.history.push("/index/home")
              
            }
        })
        
        
    }
    changeuser(v,key){
        console.log(v);
        this.setState({
            user:{
                ...this.state.user,
                [key]:v
            }
        })
 }
    render() {
        return (
            <div>
                <Header title="登录" register></Header>
                <List.Item prefix='账号：' >
                    <Input placeholder='请输入账号' clearable onChange={(v)=>this.changeuser(v,'phone')} />
                </List.Item>
                
                <List.Item prefix='密码'>
                    <Input placeholder='请输入密码' clearable type='password' onChange={(v)=>this.changeuser(v,'password')} />
                </List.Item>

                <Button color='primary' onClick={() => this.login()}>登录</Button>
                {/* <Link to="/index/home">首页</Link> */}
            </div>
        )
    }
}
